<?php
defined('IN_PHPCMS') or exit('No permission resources.');
pc_base::load_app_class('admin','admin',0);
pc_base::load_app_func('global');

class admin_daoshi extends admin {

	private $db,$db_zb,$db_zbing; public $username;
	public function __construct() {
		parent::__construct();
		//if (!module_exists(ROUTE_M)) showmessage(L('module_not_exists'));
		$this->username = param::get_cookie('admin_username');
		$this->db = pc_base::load_model('daoshi_model');
		$this->db_zb = pc_base::load_model('daoshizb_model');
		$this->db_zbing = pc_base::load_model('daoshizbing_model');
	}
	//预约列表
	public function yuyuelist()
	{
		$sql = '';
		$sql = '`siteid`=\''.$this->get_siteid().'\'';
		$sql .= $_GET['aid'] ? ' and zhibanid ='.$_GET['aid'] : '';
		$page = max(intval($_GET['page']), 1);
		$data = $this->db_zbing->listinfo($sql, '`bookingid` DESC', $page);
		$show_header=1;
		include $this->admin_tpl('announce_listbook');
	}
	public function selectdaoshi()
	{
		pc_base::load_sys_class('form', '', 0);
		$sql = $str = '';
		$catid = $_GET['catid'] ? intval($_GET['catid']) : '';
		$name = $_GET['name'] ? $_GET['name'] : '';
		$sql = '`siteid`=\''.$this->get_siteid().'\'';
		if($catid)
		{
			$sql .= get_catid_sql($catid);
		}
		$data = $this->db->select($sql);
		foreach($data as $k=>$v)
		{
			$data1[$v['daoshiid']] = $v['name'];
		}
		$t = 'show_template';
		$html[$t] = form::select($data1, '', 'name="announce[daoshiid]"');
		echo json_encode($html);
		
	}
	public function init_zb() {
		//导师列表
		$sql = '';
		$catid = $_GET['catid'] ? $_GET['catid'] : '';
		$sql = '`siteid`=\''.$this->get_siteid().'\'';
		$sql .= $_GET['daoshiid'] ? ' and daoshiid ='.$_GET['daoshiid'] : '';
		$did = intval($_GET['announce']['daoshiid']);
		if($did)
		{
			$sql .= 'and daoshiid ='.$did;
		}
		$page = max(intval($_GET['page']), 1);
		$data = $this->db_zb->listinfo($sql, '`listorder` DESC,`zhibanid` DESC', $page);
		$show_header=1;
		include $this->admin_tpl('announce_listzb');
	}
	public function zblist() {
		//导师列表
		$sql = '';
		$catid = $_GET['catid'] ? $_GET['catid'] : '';
		$sql = '`siteid`=\''.$this->get_siteid().'\'';
		$sql .= $_GET['daoshiid'] ? ' and daoshiid ='.$_GET['daoshiid'] : '';
		$did = intval($_GET['announce']['daoshiid']);
		if($did)
		{
			$sql .= 'and daoshiid ='.$did;
		}
		//echo $sql;
		$page = max(intval($_GET['page']), 1);
		$data = $this->db_zb->listinfo($sql, '`listorder` DESC,`zhibanid` DESC', $page);
				$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=daoshi&c=admin_daoshi&a=add\', title:\''.L('announce_add').'\', width:\'700\', height:\'500\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('announce_add'));
		foreach($data as $k=>$v)
		{
			$an_info = $this->db->get_one(array('daoshiid' => $v['daoshiid']));
			$data[$k]['name'] = $an_info['name'];
		}
		include $this->admin_tpl('announce_listzb1');
	}
	public function init() {
		//导师列表
		$sql = '';
		$catid = $_GET['catid'] ? intval($_GET['catid']) : '';
		$name = $_GET['name'] ? $_GET['name'] : '';
		$sql = '`siteid`=\''.$this->get_siteid().'\'';
		if($catid)
		{
			$sql .= get_catid_sql($catid);
		}
		if($name)
		{
			$sql .= " and `name` like '%".$name."%'";
		}
		//echo $sql;
		$page = max(intval($_GET['page']), 1);
		$data = $this->db->listinfo($sql, '`listorder` DESC,`daoshiid` DESC', $page);
		$big_menu = array('javascript:window.top.art.dialog({id:\'add\',iframe:\'?m=daoshi&c=admin_daoshi&a=add\', title:\''.L('announce_add').'\', width:\'700\', height:\'500\', lock:true}, function(){var d = window.top.art.dialog({id:\'add\'}).data.iframe;var form = d.document.getElementById(\'dosubmit\');form.click();return false;}, function(){window.top.art.dialog({id:\'add\'}).close()});void(0);', L('announce_add'));
		include $this->admin_tpl('announce_list');
	}
	
	/**
	 * 添加导师
	 */
	public function add() {
		if(isset($_POST['dosubmit'])) {
			$_POST['announce'] = $this->check($_POST['announce']);
			if($this->db->insert($_POST['announce'])) showmessage(L('announcement_successful_added'), HTTP_REFERER, '', 'add');
		} else {
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$siteid = $this->get_siteid();
			$template_list = template_list($siteid, 0);
			$site = pc_base::load_app_class('sites','admin');
			$info = $site->get_by_id($siteid);
			foreach ($template_list as $k=>$v) {
				$template_list[$v['dirname']] = $v['name'] ? $v['name'] : $v['dirname'];
				unset($template_list[$k]);
			}
			$show_header = $show_validator = $show_scroll = 1;
			pc_base::load_sys_class('form', '', 0);
			include $this->admin_tpl('announce_add');
		}
	}
		/**
	 * 添加导师值班
	 */
	public function add_zb() {
		if(isset($_POST['dosubmit'])) {
			$_POST['announce'] = $this->check_zhiban($_POST['announce']);
			if($this->db_zb->insert($_POST['announce'])) showmessage('添加导师值班成功', HTTP_REFERER);
		} else {
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');
			$siteid = $this->get_siteid();
			//$show_header = $show_validator = $show_scroll = 1;
			pc_base::load_sys_class('form', '', 0);
			include $this->admin_tpl('announce_add_zhiban');
		}
	}
	/**
	 * 修改导师
	 */
	public function edit() {
		$_GET['aid'] = intval($_GET['aid']);
		if(!$_GET['aid']) showmessage(L('illegal_operation'));
		if(isset($_POST['dosubmit'])) {
			$_POST['announce'] = $this->check($_POST['announce'], 'edit');
			if($this->db->update($_POST['announce'], array('daoshiid' => $_GET['aid']))) showmessage(L('announced_a'), HTTP_REFERER, '', 'edit');
		} else {
			$where = array('daoshiid' => $_GET['aid']);
			$an_info = $this->db->get_one($where);
			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');		
			$show_header = $show_validator = $show_scroll = 1;
			include $this->admin_tpl('announce_edit');
		}
	}
	public function edit_zb() {
		$_GET['aid'] = intval($_GET['aid']);
		if(!$_GET['aid']) showmessage(L('illegal_operation'));
		if(isset($_POST['dosubmit'])) {
			$_POST['announce'] = $this->check_zhiban($_POST['announce'], 'edit');
			if($this->db_zb->update($_POST['announce'], array('zhibanid' => $_GET['aid']))) showmessage(L('announced_a'), HTTP_REFERER, '', 'edit_zb');
		} else {
			$where = array('zhibanid' => $_GET['aid']);
			$an_info = $this->db_zb->get_one($where);
			$an_info1 = $this->db->get_one(array('daoshiid' => $an_info['daoshiid']));
			pc_base::load_sys_class('form', '', 0);
			//获取站点模板信息
			pc_base::load_app_func('global', 'admin');		
			$show_header = $show_validator = $show_scroll = 1;
			include $this->admin_tpl('announce_edit_zb');
		}
	}
	/**
	 * 批量修改导师排序
	 */
	public function order() {
		if((!isset($_POST['listorder']) || empty($_POST['listorder'])) && !$listorder) {
			showmessage(L('illegal_operation'));
		} else {
			
				foreach($_POST['listorder'] as $k=>$v)
				{
					$this->db->update(array('listorder' => $v), array('daoshiid' => $k));
				}
				showmessage('排序成功', HTTP_REFERER);
		}
	}
	
	public function order_zb() {
		if((!isset($_POST['listorder']) || empty($_POST['listorder'])) && !$listorder) {
			showmessage(L('illegal_operation'));
		} else {
				foreach($_POST['listorder'] as $k=>$v)
				{
					$this->db_zb->update(array('listorder' => $v), array('zhibanid' => $k));
				}
				showmessage('排序成功', HTTP_REFERER);
		}
	}
	
	/**
	 * 批量删除导师
	 */
	public function delete($aid = 0) {
		if((!isset($_POST['aid']) || empty($_POST['aid'])) && !$aid) {
			showmessage(L('illegal_operation'));
		} else {
			if(is_array($_POST['aid']) && !$aid) {
				array_map(array($this, 'delete'), $_POST['aid']);
				showmessage(L('announce_deleted'), HTTP_REFERER);
			} elseif($aid) {
				$aid = intval($aid);
				$this->db->delete(array('daoshiid' => $aid));
				$this->db_zb->delete(array('daoshiid' => $aid));
				$this->db_zbing->delete(array('daoshiid' => $aid));
			}
		}
	}
	public function delete_zb($aid = 0) {
		//print_r($_POST['aid']);exit;
		if((!isset($_POST['aid']) || empty($_POST['aid'])) && !$aid) {
			showmessage(L('illegal_operation'));
		} else {
			if(is_array($_POST['aid']) && !$aid) {
				array_map(array($this, 'delete_zb'), $_POST['aid']);
				showmessage(L('announce_deleted'), HTTP_REFERER);
			} elseif($aid) {
				$aid = intval($aid);
				$this->db_zb->delete(array('zhibanid' => $aid));
			}
		}
	}
	/**
	 * 验证表单数据
	 * @param  		array 		$data 表单数组数据
	 * @param  		string 		$a 当表单为添加数据时，自动补上缺失的数据。
	 * @return 		array 		验证后的数据
	 */
	private function check($data = array(), $a = 'add') {
		if($data['name']=='') showmessage(L('title_cannot_empty'));
		if($data['description']=='') showmessage(L('announcements_cannot_be_empty'));
		if ($a=='add') {
			$data['siteid'] = $this->get_siteid();
			$data['addtime'] = $data['updatetime'] = SYS_TIME;
			$data['username'] = $this->username;
		} else {
			$data['updatetime'] = SYS_TIME;
		}
		return $data;
	}
	
		private function check_zhiban($data = array(), $a = 'add') {
		if($data['daoshiid']=='') showmessage('请选择导师');
		if($data['address']=='') showmessage('请输入值班地点');
		if ($a=='add') {
			$data['siteid'] = $this->get_siteid();
			$data['addtime'] = $data['updatetime'] = SYS_TIME;
			$data['username'] = $this->username;
		} else {
			$data['updatetime'] = SYS_TIME;
		}
		return $data;
	}
}
?>